package com.ebt.m.customer.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.adapter.AdapterCustomerGet;
import com.ebt.m.customer.constant.CustomerConstant;
import com.ebt.m.customer.item.Contact;
import com.ebt.m.customer.listener.IBackHandlerActivity;
import com.ebt.m.customer.listener.IBackHandlerFragment;
import com.ebt.m.data.CustomerData;
import com.ebt.m.homepage.GetCustomerEvent;
import com.ebt.m.utils.StateManager;
import com.ebt.m.widget.MDEBTDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


public class FragmentCustomerGet extends Fragment implements IBackHandlerFragment {

    private static final String TAG = "wang";

    private TextView fragment_customer_get_text_view;
    private ListView fragment_customer_get_list_view;

    private AdapterCustomerGet mAdapterCustomerGet;

    private Context mContext;
    private CustomerData mCustomerData;
    private List<Contact> mContactsForCustomerGet;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initViews(inflater, container);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initValues();
    }

    public View initViews(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_customer_get, null);

        fragment_customer_get_list_view = (ListView) view.findViewById(R.id.fragment_customer_get_list_view);
        fragment_customer_get_text_view = (TextView) view.findViewById(R.id.fragment_customer_get_text_view);

        return view;
    }

    public void initValues() {

        mContext = this.getActivity();

        if (mContext instanceof IBackHandlerActivity) {
            ((IBackHandlerActivity) mContext).setSelectedFragment(this);
        }

        mCustomerData = new CustomerData(mContext);

        mContactsForCustomerGet = mCustomerData.getListOfContactForCustomerGet();

        if (mContactsForCustomerGet == null) {
            mContactsForCustomerGet = new ArrayList<>();
        }

        mAdapterCustomerGet = new AdapterCustomerGet(getActivity(), mContactsForCustomerGet);

        fragment_customer_get_list_view.setAdapter(mAdapterCustomerGet);
        fragment_customer_get_list_view.setEmptyView(fragment_customer_get_text_view);

        if (mContactsForCustomerGet.size() == 0) {
            this.getActivity().getWindow().getDecorView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showDialogForReadingContact();
                }
            }, 300);
        }
    }

    private void initWithDialogGranted(boolean updateNumber) {
        this.getActivity().getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                mContactsForCustomerGet = mCustomerData.getListOfContactForCustomerGet();

                if (mContactsForCustomerGet == null) {
                    mContactsForCustomerGet = new ArrayList<>();
                }
                mAdapterCustomerGet = new AdapterCustomerGet(getActivity(), mContactsForCustomerGet);

                fragment_customer_get_list_view.setAdapter(mAdapterCustomerGet);
                fragment_customer_get_list_view.setEmptyView(fragment_customer_get_text_view);
                if (updateNumber) {
                    EventBus.getDefault().post(new GetCustomerEvent());
                }
            }
        }, 200);
    }

    public static FragmentCustomerGet newInstance() {
        FragmentCustomerGet fragmentCustomerGet = new FragmentCustomerGet();
        return fragmentCustomerGet;
    }

    @Override
    public Object[] onBackPressed() {
        return null;
    }


    private void showDialogForReadingContact() {
        String[] messages = new String[]{"是否同意为" + getResources().getString(R.string.app_name) + "开启读取通讯录的权限？"};
        new MDEBTDialog.Builder(getContext())
                .setMessages(messages)
                .setTitle("提示")
                .setNegativeButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                })
                .setPositiveButton("同意", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            /*Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            intent.setData(Uri.fromParts("package", getActivity().getPackageName(), null));
                            startActivity(intent);*/
                            Intent intent = new Intent(Settings.ACTION_SETTINGS);
                            startActivity(intent);
                        } catch (Exception e) {
                        }
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
//                        setCanReadContacts(false);
                    }
                })
                .create()
                .show();
    }

    private boolean getIfFirstReadContacts() {
        StateManager sm = StateManager.getInstance(getContext());
        return sm.getBoolean(CustomerConstant.CustomerGet.FIRST_READ_CONTACT, true);
    }

    private boolean getIfCanReadContacts() {
        StateManager sm = StateManager.getInstance(getContext());
        return sm.getBoolean(CustomerConstant.CustomerGet.CAN_READ_CONTACT, false);
    }

    private void setFirstReadContacts(boolean first) {
        StateManager sm = StateManager.getInstance(getContext());
        sm.setBoolean(CustomerConstant.CustomerGet.FIRST_READ_CONTACT, first);
    }

    private void setCanReadContacts(boolean can) {
        StateManager sm = StateManager.getInstance(getContext());
        sm.setBoolean(CustomerConstant.CustomerGet.CAN_READ_CONTACT, can);
    }

}
